package com.aaa.dem1027.第一遍.d02;

import java.lang.reflect.Field;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/10/27  14:39
 */
public class Test1 {
    public static void main(String[] args) throws Exception, InstantiationException {
        Class<Tea> teaClass = Tea.class;
        Tea tea = teaClass.newInstance();
        System.out.println("tea = " + tea);

        //获取反射类对应得属性对象
        Field name = teaClass.getDeclaredField("name");
        name.set(tea,"汪豪杰");//通过反射默认对指定属性的对象赋值
        System.out.println("tea = " + tea);

        //通过反射获取指定属性的对象值。 tea.getName()普通获取
        Object o = name.get(tea);
        System.out.println("o = " + o);

        Field age = teaClass.getDeclaredField("age");
        age.setAccessible(true);//允许访问私有属性
        age.set(tea,22);
        System.out.println("tea = " + tea);

        Object o1 = age.get(tea);
        System.out.println("o1 = " + o1);
    }
}

class Student{
    public String address;
    private String sex;
}
class Tea extends Student {

    public String name;
    private Integer age;

    @Override
    public String toString() {
        return "Tea{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
